﻿using GridWork.Models;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;

namespace GridWork.Controllers.Api
{
    public class {subFolder}Controller  : BaseController
    {
        public IDatabase db = DbHelper.InitDatabase();
	[folder start]

		#region {tblName}模块

        public void [tblName]List()
        {

            // fields: {strNames}
            Sql sql = new Sql("select * from [tblName]");
            #region 查询条件
  [loop start]
			string [fldName] = GetString("[fldName]");
            if ([fldName] != "")
            {
                sql.Where("[fldName]=@0", [fldName]); //[fldTitle]
            }

  [loop end]^
            sql.OrderBy(" id desc");
            #endregion

            //success(sql);
            Page<[tblName]> list = db.Page<[tblName]>(1, 20, sql);
            success(list);
        }
        public void [tblName]View()
        {
            int id = GetInt("id");
            if (id <= 0)
            {
                var mod = new Models.[tblName](){ id = 0 };
                success(mod);
            }
            else
            {
                var mod = db.FirstOrDefault<[tblName]>("where id=@0", id);
                if (mod != null)
                {
                    success(mod);
                }
                else
                {
                    error("记录不存在");
                }
            }
        }
        public void [tblName]Edit()
        {
            int id = GetInt("id");
            var mod = new Models.[tblName]();
            if (id > 0)
            {
                mod = db.FirstOrDefault<[tblName]>("where id=@0", id);
                if (mod == null)
                {
                    error("要修改的记录不存在");
                }
				else
				{
					mod = GetFormModel(mod);
					if (ValidateCheck(mod))
					{
						db.Update(mod);
						success(mod);
					}
				}
            }
            else
            {
                mod = GetFormModel(mod);
                if (ValidateCheck(mod))
                {
  [loop start]
                    //mod.[fldName] = [fldDefaultValue];
  [loop end]^
                    db.Insert(mod);
                    success(mod);
                }

                db.Insert(mod);
				if(mod.id>0){
		            success(mod);
				}
				else
				{
		            error("数据插入失败");
				}
            }

        }
        public Boolean ValidateCheck([tblName] mod)
        {
            #region 数据校验
  [loop start]
            if (LIB.Check.isEmpty(mod.[fldName]))
            {
                validateError("[fldTitle]不能为空");
                return false;
            }
  [loop end]^

            #endregion
            return true;
        }
 
		public void [tblName]Delete()
        {
            int id = GetInt("id");
            if (id <= 0)
            {
                error("非法的id");
            }
            else
            {
				int rows = db.Delete<[tblName]>(id);
				if (rows > 0)
				{
					success("删除成功");
				}
				else
				{
					error("删除失败");
				}
            }

        }
		#endregion
	[folder end]
	}
}